# Start Python Server

# 保存原始工作目录
$originalDir = Get-Location

# Check and start required dependencies (MySQL and Redis)
function Check-And-Start-Dependencies {
    Write-Host "=========================================" -ForegroundColor Cyan
    Write-Host "Checking required dependencies..." -ForegroundColor Cyan
    Write-Host "=========================================" -ForegroundColor Cyan
    
    # Check and start MySQL if not running
    $mysqlRunning = docker ps | Select-String -Pattern "mysql-server"
    if (-not $mysqlRunning) {
        Write-Host "MySQL service not running. Starting MySQL..." -ForegroundColor Yellow
        try {
            . "$PSScriptRoot\01start_mysql.ps1"
            Write-Host "MySQL started successfully." -ForegroundColor Green
        } catch {
            Write-Host "Failed to start MySQL: $_" -ForegroundColor Red
            Exit 1
        }
    } else {
        Write-Host "MySQL service is already running." -ForegroundColor Green
    }
    
    # Check and start Redis if not running
    $redisRunning = docker ps | Select-String -Pattern "redis-server"
    if (-not $redisRunning) {
        Write-Host "Redis service not running. Starting Redis..." -ForegroundColor Yellow
        try {
            . "$PSScriptRoot\02start_redis.ps1"
            Write-Host "Redis started successfully." -ForegroundColor Green
        } catch {
            Write-Host "Failed to start Redis: $_" -ForegroundColor Red
            Exit 1
        }
    } else {
        Write-Host "Redis service is already running." -ForegroundColor Green
    }
    
    Write-Host "=========================================" -ForegroundColor Cyan
    Write-Host "All dependencies checked." -ForegroundColor Cyan
    Write-Host "=========================================" -ForegroundColor Cyan
}

# Check and start dependencies
Check-And-Start-Dependencies

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Starting Python Server..." -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan

# Check if Docker is running
Write-Host "Checking Docker service status..." -ForegroundColor Yellow
try {
    $dockerStatus = docker info 2>$null
    if (-not $dockerStatus) {
        Write-Host "Error: Docker service is not running. Please start Docker Desktop first." -ForegroundColor Red
        Exit 1
    }
} catch {
    Write-Host "Error: Exception occurred while checking Docker status: $_" -ForegroundColor Red
    Exit 1
}

# Switch to server directory
Write-Host "Switching to server directory..." -ForegroundColor Yellow
try {
    Set-Location -Path "$PSScriptRoot\server"
} catch {
    Write-Host "Error: Failed to switch to server directory: $_" -ForegroundColor Red
    # 恢复原始工作目录后退出
    Set-Location -Path $originalDir
    Exit 1
}

# Start Python server container
Write-Host "Starting Python server container..." -ForegroundColor Yellow
try {
    # Use docker-compose to start Python server
    docker-compose -f 7-docker-compose-python-server.yml up -d --build
    
    if ($LASTEXITCODE -eq 0) {
        Write-Host "========================================" -ForegroundColor Green
        Write-Host "Python Server started successfully!" -ForegroundColor Green
        Write-Host "Service address: http://localhost:5000" -ForegroundColor Green
        Write-Host "========================================" -ForegroundColor Green
        
        # Display container status
        Write-Host "Python Server container status:" -ForegroundColor Yellow
        docker ps --filter "name=python-server" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
    } else {
        Write-Host "Error: Failed to start Python Server. Please check logs for details." -ForegroundColor Red
        docker-compose -f 7-docker-compose-python-server.yml logs --tail 50
        Exit 1
    }
} catch {
    Write-Host "Error: Exception occurred while starting Python Server: $_" -ForegroundColor Red
    Exit 1
}

# Instructions for viewing logs
Write-Host "" -ForegroundColor White
Write-Host "View Python Server logs: docker-compose -f $PSScriptRoot\server\7-docker-compose-python-server.yml logs -f" -ForegroundColor Yellow
Write-Host "Stop Python Server: docker-compose -f $PSScriptRoot\server\7-docker-compose-python-server.yml down" -ForegroundColor Yellow
Write-Host "" -ForegroundColor White

# 恢复原始工作目录
Set-Location -Path $originalDir